package com.wx.web.controller.trade;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.business.course.IxbTrainingAgencyService;
import com.wx.business.course.OrderRecordService;
import com.wx.business.trade.IxbTradeRecordService;
import com.wx.business.util.OrderNoGenerater;
import com.wx.business.util.XMathUtil;
import com.wx.model.AppConfig;
import com.wx.model.IxbTradeRecord;
import com.wx.model.IxbTrainingAgency;
import com.wx.model.IxbUser;
import com.wx.model.OrderRecord;
import com.wx.model.OrderRecordExample;

/**
 * 订单控制器
 */
@Controller
@RequestMapping("/order")
public class OrderRecordController {

	
	@Autowired
	public IxbTradeRecordService ixbTradeRecordService;
	
	@Autowired
	public  OrderRecordService   orderRecordService;
	
	@Autowired
	public  IxbCourseDetailService   ixbCourseDetailService;
	
	 @Autowired
	 private IxbTrainingAgencyService   agencyService;
    
	 
	 DecimalFormat df = new DecimalFormat("0.0#");
	 
	/**
	 * 创建订单
	 * @param buyerId
	 * @param buyerName
	 * @param courseId
	 * @param courseName
	 * @param tradeFee
	 * @return
	 */
    @RequestMapping(value = "/h5/create")
    @ResponseBody
    public JsonResult create(HttpServletRequest request ,String courseId) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
        Map<String,Object>  courseDetailMap=ixbCourseDetailService.queryCourseDetailById(courseId);
        
        //原始价格
        double  tradeFee=Double.parseDouble(courseDetailMap.get("coursePrice")+"");
        //优惠折扣
        double  discount=Double.parseDouble(courseDetailMap.get("discount")+"");
        //最后价格
        BigDecimal  lastTradeFee=XMathUtil.multiply( new BigDecimal(tradeFee), new BigDecimal(discount)).setScale(2, BigDecimal.ROUND_HALF_UP);
        
        BigDecimal discountFee=XMathUtil.subtract(new BigDecimal(tradeFee), lastTradeFee);
        String courseTitle=courseDetailMap.get("courseTitle")+"";
        String courseIntro=courseDetailMap.get("courseIntro")+"";
        String agencyId=courseDetailMap.get("publisherId")+"";
        //订单号
        String orderNo=OrderNoGenerater.getInstance().generaterCourseOrderNo();
        
        //查询机构
        IxbTrainingAgency agency= agencyService.selectByPrimaryKey(agencyId);

        IxbTradeRecord record=null;
		try {
			record=ixbTradeRecordService.createTradeRecord(user.getSid(), user.getUserName(), courseId, courseTitle, lastTradeFee.doubleValue(),orderNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		OrderRecord courseOrder=orderRecordService.createOrder(agencyId, user.getSid(), courseId, AppConfig.BaseUrl+courseDetailMap.get("businessPic"), orderNo, agency.getAgencyName(), courseTitle, courseIntro, tradeFee, lastTradeFee.doubleValue());
		
        Map<String,Object> dataMap=new HashMap<String, Object>();
        dataMap.put("discountFee",df.format(discountFee));
        dataMap.put("payNo", record.getTradeNo());
        dataMap.put("courseOrder", courseOrder);
        return new JsonResult(true, dataMap);
    }

    /**
     * 用户订单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/h5/getOrderList")
    @ResponseBody
    public JsonResult getOrderList(HttpServletRequest request ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		 OrderRecordExample orderExample=new OrderRecordExample();
		 orderExample.createCriteria().andBuyerIdEqualTo(user.getSid());
		
		 Pagination<OrderRecord> pageList=orderRecordService.queryByPage(orderExample, 0, 100, " order_create_time desc");
		 List<OrderRecord> orderList=pageList.getDataList();
		 return new JsonResult(true, orderList);		
		
    }
    
    
    /**
     * 订单详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/h5/getOrderDetail")
    @ResponseBody
    public JsonResult getOrderDetail(HttpServletRequest request ,String orderId) {
		
		 Map<String,Object> orderDetail=orderRecordService.selectOrderMapById(orderId);
		 if(orderDetail==null)
		 {
			 return new JsonResult(false, "订单不存在");	 
		 }
		 String  discountFee= XMathUtil.subtract(orderDetail.get("goodsFee")+"",orderDetail.get("orderFee")+"");
		 orderDetail.put("discountFee", discountFee);
		 return new JsonResult(true, orderDetail);		
		
    }
}
